/*
	This file is part of the OdinMS Maple Story Server
    Copyright (C) 2008 ~ 2010 Patrick Huy <patrick.huy@frz.cc> 
                       Matthias Butz <matze@odinms.de>
                       Jan Christian Meyer <vimes@odinms.de>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License version 3
    as published by the Free Software Foundation. You may not use, modify
    or distribute this program under any other version of the
    GNU Affero General Public License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package server.maps;

import java.awt.Point;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import client.PurePlayer;
import client.PureAccount;
import community.PurePartyCharacter;
import packet.creators.MainPacketCreator;
import java.lang.ref.WeakReference;

public class PureDoor extends AbstractmapleMapObject {

    private WeakReference<PurePlayer> owner;
    private PureMap town;
    private PurePortal townPortal;
    private PureMap target;
    private Point targetPosition;

    public PureDoor(final PurePlayer owner, final Point targetPosition) {
	super();
	this.owner = new WeakReference<PurePlayer>(owner);
	this.target = owner.getMap();
	this.targetPosition = targetPosition;
	setPosition(this.targetPosition);
	this.town = this.target.getReturnMap();
	this.townPortal = getFreePortal();
    }

    public PureDoor(final PureDoor origDoor) {
	super();
	this.owner = origDoor.owner;
	this.town = origDoor.town;
	this.townPortal = origDoor.townPortal;
	this.target = origDoor.target;
	this.targetPosition = origDoor.targetPosition;
	this.townPortal = origDoor.townPortal;
	setPosition(townPortal.getPosition());
    }

    private final PurePortal getFreePortal() {
        PurePlayer realOwner = owner.get();
        if (realOwner != null) {
            final List<PurePortal> freePortals = new ArrayList<PurePortal>();

            for (final PurePortal port : town.getPortals()) {
                if (port.getType() == 6) {
                    freePortals.add(port);
                }
            }
            Collections.sort(freePortals, new Comparator<PurePortal>() {

                @Override
                public final int compare(final PurePortal o1, final PurePortal o2) {
                    if (o1.getId() < o2.getId()) {
                        return -1;
                    } else if (o1.getId() == o2.getId()) {
                        return 0;
                    } else {
                        return 1;
                    }
                }
            });
            for (final PureMapObject obj : town.getAllDoor()) {
                final PureDoor door = (PureDoor) obj;
                if (door.getOwner().getParty() != null
                        && realOwner.getParty().containsMembers(new PurePartyCharacter(door.getOwner()))) {
                    freePortals.remove(door.getTownPortal());
                }
            }

            return freePortals.iterator().next();
        }
        return null;
    }

    @Override
    public final void sendSpawnData(final PureAccount client) {
	if (getOwner() == null || target == null || client.getPlayer() == null) {
	    return;
	}
        if (target.getId() == client.getPlayer().getMapId() || getOwner().getId() == client.getPlayer().getId() || (getOwner() != null && getOwner().getParty() != null && client.getPlayer().getParty() != null && getOwner().getParty().getId() == client.getPlayer().getParty().getId())) {
            client.getSession().write(MainPacketCreator.spawnDoor(getOwner().getId(), target.getId() == client.getPlayer().getMapId() ? targetPosition : townPortal.getPosition(), true)); //spawnDoor always has same position.
            if (getOwner() != null && getOwner().getParty() != null && client.getPlayer().getParty() != null && (getOwner().getId() == client.getPlayer().getId() || getOwner().getParty().getId() == client.getPlayer().getParty().getId())) {
                client.getSession().write(MainPacketCreator.partyPortal(town.getId(), target.getId(), 2311002, target.getId() == client.getPlayer().getMapId() ? targetPosition : townPortal.getPosition(), true));
            }
            client.getSession().write(MainPacketCreator.spawnPortal(town.getId(), target.getId(), 2311002, target.getId() == client.getPlayer().getMapId() ? targetPosition : townPortal.getPosition()));
        }
    }


    @Override
    public final void sendDestroyData(final PureAccount client) {
	if (client.getPlayer() == null || getOwner() == null || target == null) {
	    return;
	}
	if (target.getId() == client.getPlayer().getMapId() || getOwner().getId() == client.getPlayer().getId() || (getOwner() != null && getOwner().getParty() != null && client.getPlayer().getParty() != null && getOwner().getParty().getId() == client.getPlayer().getParty().getId())) {
            client.getSession().write(MainPacketCreator.removeDoor(getOwner().getId(), true));
            if (getOwner() != null && getOwner().getParty() != null && client.getPlayer().getParty() != null && (getOwner().getId() == client.getPlayer().getId() || getOwner().getParty().getId() == client.getPlayer().getParty().getId())) {
                client.getSession().write(MainPacketCreator.partyPortal(999999999, 999999999, 0, new Point(-1, -1), false));
            }
	    client.getSession().write(MainPacketCreator.spawnPortal(999999999, 999999999, 0, null));
        }
    }

    public final void warp(final PurePlayer chr, final boolean toTown) {
        if (getOwner() == null) {
            return;
        }
	if (chr == getOwner() || getOwner().getParty() != null && getOwner().getParty().containsMembers(new PurePartyCharacter(chr))) {
	    if (!toTown) {
		chr.changeMap(target, targetPosition);
	    } else {
		chr.changeMap(town, townPortal);
	    }
	} else {
	    chr.getClient().getSession().write(MainPacketCreator.resetActions());
	}
    }

    public final PurePlayer getOwner() {
	return owner.get();
    }

    public final PureMap getTown() {
	return town;
    }

    public final PurePortal getTownPortal() {
	return townPortal;
    }

    public final PureMap getTarget() {
	return target;
    }

    public final Point getTargetPosition() {
	return targetPosition;
    }

    @Override
    public final PureMapObjectType getType() {
	return PureMapObjectType.DOOR;
    }
}
